package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Collect;
import com.ruoyi.system.domain.vo.EvaluateVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Evaluate;
import com.ruoyi.system.service.IEvaluateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 留言Controller
 * 
 * @author ruoyi
 * @date 2024-03-10
 */
@RestController
@RequestMapping("/system/evaluate")
public class EvaluateController extends BaseController
{
    @Autowired
    private IEvaluateService evaluateService;

    /**
     * 查询留言列表
     */
    //@PreAuthorize("@ss.hasPermi('system:evaluate:list')")
    @GetMapping("/list")
    public TableDataInfo list(Evaluate evaluate)
    {
        startPage();
        List<EvaluateVo> evaluateVos = evaluateService.selectEvaluateList(evaluate);
        return getDataTable(evaluateVos);
    }

    /**
     * 导出留言列表
     */
    //@PreAuthorize("@ss.hasPermi('system:evaluate:export')")
//    @Log(title = "留言", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Evaluate evaluate)
//    {
//        List<Evaluate> list = evaluateService.selectEvaluateList(evaluate);
//        ExcelUtil<Evaluate> util = new ExcelUtil<Evaluate>(Evaluate.class);
//        util.exportExcel(response, list, "留言数据");
//    }

    /**
     * 获取留言详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:evaluate:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(evaluateService.selectEvaluateById(id));
//    }

    /**
     * 新增留言
     */
    //@PreAuthorize("@ss.hasPermi('system:evaluate:add')")
    @Log(title = "留言", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Evaluate evaluate)
    {
        if (ObjectUtils.isEmpty(evaluate.getCommodityId())){
            return error("商品id不能为空");
        }
        if (!ObjectUtils.isEmpty(evaluate.getEvaluateId())){
            if (evaluateService.selectEvaluateById(evaluate.getEvaluateId())==null) {
                return error("留言id无效");
            }
        }
        evaluate.setPublisherId(getUserId());
        return toAjax(evaluateService.insertEvaluate(evaluate));
    }

    /**
     * 修改留言
     */
    //@PreAuthorize("@ss.hasPermi('system:evaluate:edit')")
//    @Log(title = "留言", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody Evaluate evaluate)
//    {
//        return toAjax(evaluateService.updateEvaluate(evaluate));
//    }

    /**
     * 删除留言
     */
    //@PreAuthorize("@ss.hasPermi('system:evaluate:remove')")
    @Log(title = "留言", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for(Long id:ids){
            Evaluate evaluate = evaluateService.selectEvaluateById(id);
            if (evaluate==null) {
                return error("包含无效的id值！");
            }
            if (!getUserId().equals(evaluate.getPublisherId())){
                return error("包含非法的id值！");
            }
        }
        int sum=0;
        for(Long id:ids){
            Evaluate evaluate = evaluateService.selectEvaluateById(id);
            Evaluate del=new Evaluate();
            del.setEvaluateId(evaluate.getId());
            del.setCommodityId(evaluate.getCommodityId());
            int i = evaluateService.deleteEvaluate(del);
            int j = evaluateService.deleteEvaluateById(id);
            sum=i+j;
        }
        return toAjax(sum);
    }
}
